package com.spring.schedule_system.dengxiaojiao.repositoriesImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.schedule_system.dengxiaojiao.mapper.VenDbDeliverySetMapper;
import com.spring.schedule_system.dengxiaojiao.repositories.VenDbDeliverySetRepositories;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetDomainIn;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetDomainOut;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetSaveDomainIn;
import com.spring.schedule_system.dengxiaojiao.request.VenDbDeliverySet;
import com.spring.schedule_system.dengxiaojiao.util.BizException;
import com.spring.schedule_system.dengxiaojiao.util.BusinessContext;
import com.spring.schedule_system.dengxiaojiao.util.ListResult;
import com.spring.schedule_system.dengxiaojiao.util.LyListResultUtil;
import com.spring.schedule_system.dengxiaojiao.util.OptResult;
import com.spring.schedule_system.dengxiaojiao.util.StringUtil;
import com.szlanyou.cloud.uid.utils.LyIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/***********************************************************************************************************************
* <p>
    * 预交车天数设置 细分biz与数据库交互接口实现层
    * </p>
* @author        ly-dengxiaojiao
* @date          2023-03-26
* @version       V1.0
**********************************************************************************************************************/
@Component
public class VenDbDeliverySetRepositoriesImpl extends ServiceImpl<VenDbDeliverySetMapper, VenDbDeliverySet> implements VenDbDeliverySetRepositories {

    @Autowired
    VenDbDeliverySetMapper venDbDeliverySetMapper;


    @Override
    public OptResult venDbCarDeliveryDaySetSave(DeliverySetSaveDomainIn deliverySetSaveDomainIn){
        return null;
    }

    public  List<VenDbDeliverySet> saveOrUpdateCar(List<VenDbDeliverySet> saveDatas) {
        HashMap<String, List<VenDbDeliverySet>> map = new HashMap<>();
        List<VenDbDeliverySet> adddeliverySetListVenDb = new ArrayList<>();
        List<String> dlrIds = saveDatas.stream().map(VenDbDeliverySet::getDlrId).collect(Collectors.toList());
        QueryWrapper<VenDbDeliverySet> deliverySetEntityQueryWrapper = new QueryWrapper<>();
        deliverySetEntityQueryWrapper.lambda().in(VenDbDeliverySet::getDlrId, dlrIds);
        List<VenDbDeliverySet> venDbDeliverySets = venDbDeliverySetMapper.selectList(deliverySetEntityQueryWrapper);
        List<String> carCroutSetIds = venDbDeliverySets.stream().map(VenDbDeliverySet::getCaroutSetId).collect(Collectors.toList());
        if (!StringUtil.IsEmptyOrNull(carCroutSetIds)) {
            int i = venDbDeliverySetMapper.deleteDeliverSetBatch(carCroutSetIds);
            if (i != carCroutSetIds.size()) {
                throw BizException.newBizException("500", "500");
            }
        }

        for (VenDbDeliverySet venDbDeliverySet : saveDatas) {
                venDbDeliverySet.setCaroutSetId(LyIdUtils.genId());
                StringUtil.initUserInfo(venDbDeliverySet, StringUtil.SaveOrUpdate.Save);
                adddeliverySetListVenDb.add(venDbDeliverySet);
        }
        return adddeliverySetListVenDb;
    }

    @Override
    public ListResult<DeliverySetDomainOut> venDbCarDeliveryDaySetQuery(DeliverySetDomainIn deliverySetDomainIn){
        deliverySetDomainIn.setOemId(BusinessContext.getBusinessContext().getOemId());
        deliverySetDomainIn.setGroupId(BusinessContext.getBusinessContext().getGroupId());
        if (deliverySetDomainIn.getCarBrandCode().contains(",")) {
            deliverySetDomainIn.setCarBrandCode(null);
        }
        IPage<DeliverySetDomainOut> page = new Page<>(deliverySetDomainIn.getPageIndex(),deliverySetDomainIn.getPageSize());
        List<DeliverySetDomainOut> records = venDbDeliverySetMapper.venDbCarDeliveryDaySetQuery(page,deliverySetDomainIn);
        return LyListResultUtil.success((int) page.getCurrent(), (int) page.getPages(), (int) page.getTotal(), records, "查询成功");
    }

    @Override
    public OptResult venDbCarDeliveryDaySetQueryCount(DeliverySetDomainIn deliverySetDomainIn){
        deliverySetDomainIn.setOemId(BusinessContext.getBusinessContext().getOemId());
        deliverySetDomainIn.setGroupId(BusinessContext.getBusinessContext().getGroupId());
        int count = venDbDeliverySetMapper.venDbCarDeliveryDaySetQueryCount(deliverySetDomainIn);
        OptResult optResult = new OptResult();
        optResult.setMsg(count + "");
        return optResult;
    }

    @Override
    public int venDbDeliverySetMapperAddVenDbCarDeliveryDaySetSave(List<VenDbDeliverySet> venDbDeliverySets) {
        return venDbDeliverySetMapper.addVenDbCarDeliveryDaySetSave(venDbDeliverySets);
    }

    @Override
    public List<VenDbDeliverySet> venDbDeliverySetMapperSaveOrUpdateCar(List<VenDbDeliverySet> saveDatas) {
        return null;
    }

    @Override
    public List<DeliverySetDomainOut> venDbDeliverySetMapperVenDbCarDeliveryDaySetQuery(Object o, DeliverySetDomainIn deliverySetDomainIn) {
        return venDbDeliverySetMapper.venDbCarDeliveryDaySetQuery(null,deliverySetDomainIn);
    }

    @Override
    public List<VenDbDeliverySet> venDbDeliverySetMapperSelectList(QueryWrapper<VenDbDeliverySet> deliverySetEntityQueryWrapper) {
        return  venDbDeliverySetMapper.selectList(deliverySetEntityQueryWrapper);
    }

    @Override
    public int venDbDeliverySetMapperDeleteDeliverSetBatch(List<String> carCroutSetIds) {
        return  venDbDeliverySetMapper.deleteDeliverSetBatch(carCroutSetIds);
    }

}